package cn.geekhalo.auth.service.impl;

import cn.geekhalo.auth.domain.SysUser;
import cn.geekhalo.auth.repository.SysUserRepository;
import cn.geekhalo.auth.service.ISysUserService;
import cn.geekhalo.common.constants.CodeEnum;
import cn.geekhalo.common.exception.BusinessException;
import cn.geekhalo.common.service.AbstractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class SysUserServiceImpl extends AbstractService implements ISysUserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysUserServiceImpl.class);

    protected SysUserServiceImpl() {
        super(LOGGER);
    }

    @Autowired
    private SysUserRepository sysUserRepository;


    @Override
    public Optional<SysUser> findById(Long id) {
        return sysUserRepository.findById(id);
    }

    @Override
    public void createUser(String email, String username) {
        creatorFor(sysUserRepository)
                .instance(() -> new SysUser(email,username))
                .onError((a,e)-> {throw new BusinessException(CodeEnum.SaveError);})
                .call();
    }


}
